﻿package com.xuanque.mahjongDice 
{
	import com.xuanque.mahjongDice.control.*;
	import com.xuanque.mahjongDice.display.*;
	import com.xuanque.mahjongDice.events.ResizeEvent;
	import com.xuanque.mahjongDice.util.SignalAction;
	import flash.utils.Timer;
	
	import datas.Player;
	
	import flash.display.*;
	import flash.events.*;
	
	import utils.transitions.*;
	
	import flash.system.ApplicationDomain;
	
	import com.xuanque.mahjongDice.util.ToolTip;
	
	/**
	 * ...
	 * @author DefaultUser (Tools -> Custom Arguments...)
	 */
	public class Main extends Sprite
	{
		
		
		private var isGameResult:Boolean = false;
		
		private var gameResult:GameResult;
		
		private var resultTimer:Timer;
		
		static private var Instance:Main;
		
		/*
		private var myself:Player; //测试用户
		private var enter0:Player;
		private var enter2:Player;
		private var enter3:Player;
		*/
		public function Main() 
		{
			Instance = this;
			
			com.xuanque.mahjongDice.control.GameController.getInstance();
			
			this.root.loaderInfo.addEventListener(Event.COMPLETE, loadComplete);
			
		}
		
		/*
		 * 单件类的实例 
		*/
		public static function getInstance():Main
        {
            return Instance;
        }
		
		
		private function loadComplete(event:Event):void {
			
			ToolTip.getInstance().setStage(this.stage);
			
			ToolTip.getInstance().BindDO(min_btn, "缩小");
            ToolTip.getInstance().BindDO(max_btn, "还原");
		    ToolTip.getInstance().BindDO(out_btn, "关闭");
			//ToolTip.getInstance().BindDO(SoundCtrl.getInstance().getSoundMc(),"关闭背景音乐");
			ToolTip.getInstance().BindDO(signal_mc, "网络良好");
			
			
			//this.infoPanel.playerList.scrollbarX.init();
			//this.infoPanel.playerList.scrollbarY.init();
			//this.infoPanel.playerList.init();
			//this.infoPanel.chatWindow.init();
			
			//init();
			//addListener();
			
		}
		
		
		public function init():void {
			
			
			this.infoPanel.playerList.scrollbarX.init();
			this.infoPanel.playerList.scrollbarY.init();
			this.infoPanel.playerList.init();
			this.infoPanel.chatWindow.init();

			
			new GameAction(gameAreaControl, infoPanel , this); // 主要内部方法
			
			new SignalAction(signal_mc);
			new ImgAction(gameAreaControl);
			
			
			
			
			
			/*
			// 本人 坐下 (显示是否已经有用户在房间内)
			
			myself = new Player();
			myself.setLoginName("lbynet 1");
			myself.setPosition(1); 
			myself.setSeed(99999);
			myself.setGrading(10);//等级
			myself.setPhoto("head/man/1.png");
			
			enter2 = new Player();
			enter2.setLoginName("enter2");
			enter2.setPosition(2);
			enter2.setSeed(2222222);
			enter2.setGrading(22);//等级
			enter2.setPhoto("head/man/2.png");
			
			enter0 = new Player();
			enter0.setLoginName("enter0");
			enter0.setPosition(0);
			enter0.setSeed(100000);
			enter0.setGrading(10);//等级
			enter0.setPhoto("head/man/10.png");
			
			enter3 = new Player();
			enter3.setLoginName("enter3");
			enter3.setPosition(3);
			enter3.setSeed(3333333);
			enter3.setGrading(33);//等级
			enter3.setPhoto("head/man/3.png");
			*/
			
			
			
			
			
			/*
			
			GameController.getInstance().updateSignal();//更新信号
			
			
			var enter_players:Array = [ myself ,enter3,enter0];
			
			GameController.getInstance().enterGame( enter_players ); //第一次进入游戏,初始化用户界面
			
			GameController.getInstance().addGame(enter2);
			//GameController.getInstance().leaveGame(enter3);
			
			GameController.getInstance().leaveGame(enter2);
			GameController.getInstance().addGame(enter2);
			
			//GameController.getInstance().leaveGame(enter3);
			//GameController.getInstance().leaveGame(enter0);
			
			GameController.getInstance().setUserReady("enter0"); //准备
			//GameController.getInstance().setUserReady("lbynet 1"); 
			
			//GameController.getInstance().host("enter0"); //确定庄
			
			*/
			
			
			/*
			   首发牌给各个用户  [ [1, 1, 1, 2, 2, 3, 6], [2, 2, 2, 2, 2, 3, 6], [3, 3, 3, 2, 2, 3, 6],[4, 4, 4, 2, 2, 3, 6] ]
			
			var faPai_array:Array = [ 
									
									[ myself.getPosition() ,[1, 1, 1, 2, 2, 3, 6]],
									[ 2 , [2, 2, 2, 2, 2, 3, 6] ],
									[ 0 , [4, 4, 4, 2, 2, 3, 6] ],
									[3, 3, 3, 2, 2, 3, 6]
									
									];
			*/
			
			
			/*
			
			GameController.getInstance().gameStart( 1 ,MahJongDice.getRandomArray() ); //测试本人的牌
			GameController.getInstance().gameStart( 2 , [ 6, 2, 2, 2, 2, 2, 3 ] ); 
			GameController.getInstance().gameStart( 0 , [ 6, 4, 4, 4, 2, 2, 3 ] ); 
			GameController.getInstance().gameStart( 3 , [3, 3, 3, 2, 2, 3, 6] ); 
			
			GameController.getInstance().typeOutControl(); //发骰结束
			
			//GameController.getInstance().displayGetPai("enter2",6);//通知摇出的骰 ( 显示 取得的 牌 )
			//GameController.getInstance().displayOutPai("enter2", 2);//出骰通知 ( 显示 发出去的 牌 )
			GameController.getInstance().displayGetPai("lbynet 1", 6);//通知摇出的骰 ( 显示 取得的 牌 )
			
			
			//GameController.getInstance().getSystemPai(1); //打公骰
			//GameController.getInstance().priority("lbynet 1");//打骰 接力
			
			//GameController.getInstance().huPaiTip("lbynet 1");//胡牌提示 和其按钮控制
			//GameController.getInstance().trusteeship("enter2")//是否托管
			
			
			
			var results_array:Array = [ 
										[ myself.getLoginName() , 100],
										[ "enter2" , 200 ],
										[ "enter0" , -100 ],
										[ "enter3" , 300 ],
									  ];
			
			GameController.getInstance().showResults(results_array);//结算结果
			GameController.getInstance().gameOver();//游戏结束，可以重新开始
			
			
			GameController.getInstance().chating( "enter3" , "重新开始!" );
			*/
			
			
			
			
			/*
			//[ myself , [MahJongDice.getRandomArray() ,0 ] ],
			//                          player对象         对应：牌         状态  托管
			var hash_array:Array = [ 
										[ myself , [ [1, 1, 1, 2, 2, 3, 6 ,6] , 2 , false ] ],
										[ enter2 , [ [6, 2, 2, 2, 2, 2, 3] , -1 , true ] ],
										[ enter0 , [ [6, 4, 4, 4, 2, 2, 3] , -1 , false ] ],
										[ enter3 , [ [3, 3, 3, 2, 2, 3, 6] , -1 , false ] ],
									];
			
			
			var _hashMap:HashMap = new HashMap();
			for (var i = 0; i < hash_array.length; i++) {
				
				_hashMap.put( hash_array[i][0] , hash_array[i][1] );
			}
			
			
			
			GameController.getInstance().resetScene( _hashMap );
			
			
			//GameController.getInstance().outOfGame("enter2");
			//GameController.getInstance().userComeBack(enter2);
			*/
			
			
			
			/*
			GameController.getInstance().setUserReady("test 3");
			GameController.getInstance().setUserReady("test 0");
			
			GameController.getInstance().outOfGame("test 2"); //掉线与强制退出
			GameController.getInstance().outOfGame("test 2",false); //掉线与强制退出
			
			*/
			
			
			
			
			
			
		}
		
		
		public function addListener() {
			
			this.infoPanel.unfoldBar.addEventListener(ResizeEvent.RESIZE, resizeEventHandler);
			
			this.infoPanel.rule_btn.addEventListener(MouseEvent.CLICK, ruleHandler);
			this.infoPanel.out_btn.addEventListener(MouseEvent.CLICK, outOfGame);
			
			this.out_btn.addEventListener(MouseEvent.CLICK, outOfGame);
			this.min_btn.addEventListener(MouseEvent.CLICK, minWinHandler);
			this.max_btn.addEventListener(MouseEvent.CLICK, maxWinHandler);
			
		}
		private function ruleHandler(event:MouseEvent):void {
			
			trace(event.target);
		}
		
		
		public function minWinHandler(event:MouseEvent):void {
			ApplicationDomain.currentDomain.getDefinition("loadswf.MainLoad").minWindows();
			min_btn.mouseEnabled = false;
			max_btn.mouseEnabled = true;
            
		}
		
		public function maxWinHandler(event:MouseEvent):void {
            ApplicationDomain.currentDomain.getDefinition("loadswf.MainLoad").maxWindows();
            min_btn.mouseEnabled = true;
			max_btn.mouseEnabled = false;
			
		}
		
		
		
		
		/*
		 * 结算结果
		*/
		public function showResults(result_array:Array):void {
			
			this.isGameResult = true;
			this.gameResult = new GameResult(); // 创建存储结束的列表
			this.gameResult.x = 390;
			this.gameResult.y = -155;
			this.addChild(this.gameResult);
			
			this.gameResult.addPlayerUser(result_array);// 添加
			
			GameAction.getInstance().updateResultCard(result_array); //更新游戏结算卡
			
			Tweener.addTween(this.gameResult, { y: stage.stageHeight / 2, time:.5 } );
			
			this.resultTimer = new Timer(10000);
			this.resultTimer.addEventListener(TimerEvent.TIMER, timerHandler);
			this.resultTimer.start();
			
			this.gameResult.reStart_btn.mouseEnabled = false; // 这里停2S等服务器通知可以被点击
			this.gameResult.reStart_btn.alpha = .5;
			
		}
		private function timerHandler(event:Event):void {
			
			this.gameAreaControl.resulttSice.gotoAndStop(1);
			this.resultTimer.stop();
			this.resultTimer.removeEventListener(TimerEvent.TIMER, timerHandler);
			
			
			this.gameResult.reStart_btn.mouseEnabled = false;
			this.gameResult.out_btn.mouseEnabled = false;
			
			this.gameResult.removeAllItem();// 清除游戏结束显示列表
			
			Tweener.addTween(this.gameResult, { y: -155, time:.8 ,onComplete:this.removeGameResult} ); //清除显示列表
			
			
			GameAction.getInstance().startReadyClock();
			var ready_mc:MovieClip = this.gameAreaControl.player0.ready_mc;
			ready_mc.gotoAndStop(1);
			ready_mc.visible = true;
			ready_mc.x = -10;
			ready_mc.y = -45;
			ready_mc.addEventListener(MouseEvent.CLICK, readyHandler); //开始 按钮被单击
			
		}
		private function readyHandler(event:MouseEvent):void {
			
			var ready_mc:MovieClip = this.gameAreaControl.player0.ready_mc;
			ready_mc.removeEventListener(MouseEvent.CLICK, readyHandler);
			ready_mc.x = 58;
			ready_mc.y = -6;
			
			GameAction.getInstance().reSet();
			ready_mc.dispatchEvent(new MouseEvent(MouseEvent.CLICK));//直接发布一个事件
			
			
		}
		
		
		/*
		 * 游戏结束，可以重新开始
		*/
		public function gameOver():void {
			
			this.gameResult.reStart_btn.mouseEnabled = true; // 这里停2S等服务器通知可以被点击
			this.gameResult.reStart_btn.alpha = 1;
			
			this.gameResult.reStart_btn.addEventListener(MouseEvent.CLICK, reStartHandler);
			this.gameResult.out_btn.addEventListener(MouseEvent.CLICK, outOfGame);
			
		}
		
		
		
		
		
		/*
		 *  重新开始游戏按钮侦听器函数
		*/
		private function reStartHandler(event:MouseEvent):void {
			
			this.resultTimer.stop();
			this.resultTimer.removeEventListener(TimerEvent.TIMER, timerHandler);
			
			this.gameResult.removeAllItem();// 清除游戏结束显示列表
			
			Tweener.addTween(this.gameResult, { y: -155, time:.5 ,onComplete:this.removeGameResult} ); //清除显示列表
			//Tweener.addTween(this.gameAreaControl, { alpha: 1, time:.2 } );
			
			//reSet();
			GameAction.getInstance().reSet();
			
			this.gameAreaControl.player0.ready_mc.dispatchEvent(new MouseEvent(MouseEvent.CLICK));//直接发布一个事件
			
			/*
			//测试
			GameController.getInstance().leaveGame(enter0);
			//GameController.getInstance().addGame(enter0);
			
			GameController.getInstance().leaveGame(enter3);
			*/
			//GameController.getInstance().gameStart( 1 ,MahJongDice.getRandomArray() ); //测试本人的牌
			//GameController.getInstance().gameStart( 2 , [ 6, 2, 2, 2, 2, 2, 3 ] ); 
			//GameController.getInstance().gameStart( 0 , [ 6, 4, 4, 4, 2, 2, 3 ] ); 
			//GameController.getInstance().gameStart( 3 , [3, 3, 3, 2, 2, 3, 6] ); 
			
			//GameController.getInstance().typeOutControl(); //发骰结束
			
			//GameController.getInstance().displayGetPai("enter2",6);//通知摇出的骰 ( 显示 取得的 牌 )
			//GameController.getInstance().displayOutPai("enter2", 2);//通知摇出的骰 ( 显示 取得的 牌 )
			//GameController.getInstance().displayGetPai("lbynet 1", 6);//通知摇出的骰 ( 显示 取得的 牌 )
			
			
			//GameController.getInstance().getSystemPai(1); //打公骰
			//GameController.getInstance().priority("lbynet 1");//打骰 接力
			//GameController.getInstance().huPaiTip("lbynet 1");//胡牌提示 和其按钮控制
			//GameController.getInstance().trusteeship("enter2")//是否托管
			
			
			//GameController.getInstance().setUserReady("enter2");//设置准备
			
			
		}
		
		private function removeGameResult() {
			
			this.isGameResult = false;
			this.removeChild(this.gameResult);
		}
		
		private function outOfGame(event:MouseEvent):void {
			
			var _btn:* = event.target;
			
			trace(" +++++++++ outOfGame +++++++++  发包 ");
			
			if (this.resultTimer != null) {
				
				this.resultTimer.stop();
				this.resultTimer.removeEventListener(TimerEvent.TIMER, timerHandler);
			}
			
			
			SendPackage.getInstance().sendLeave() //发包
			
		}
		
		/*
		 *  清空所有静态属性
		*/
		public function removeFromStage():void {
			
			GameAction.getInstance().setStaticObjectNull();
			
			this.removeDisplayAllChild(this);
			
			//trace("this.numChildren == " + this.numChildren );
		}
		
		
		private function removeDisplayAllChild(displayC:DisplayObjectContainer):void{
			var disOb:DisplayObject;
			var disObC:DisplayObjectContainer;
			 while(displayC.numChildren>0){
			   disOb = displayC.getChildAt(0);
			   if(disOb is DisplayObjectContainer){
			   	 disObC = disOb as DisplayObjectContainer;
			   	 if(disObC.numChildren>1){
			   	   removeDisplayAllChild(disObC);
			   	 }else{
			       displayC.removeChild(disOb);
			   	 }
			   }else{
			     displayC.removeChild(disOb);
			   }
			 }
		}
		
		
		
		/*
		 *  resize
		*/
		private function resizeEventHandler(event:ResizeEvent):void {
			
			//trace(event.isUndold);
			if(event.isUndold == true){
				Tweener.addTween(this.gameAreaControl, { x: 500, time:.2 } );
				
				Tweener.addTween(this.middle_mc, { x: 480, time:.2 } );
				Tweener.addTween(this.rb_mc, { x: 980, time:.2 } );
				Tweener.addTween(this.rt_mc, { x: 980, time:.2 } );
				
				Tweener.addTween(this.gameAreaControl.control_btns, { x: 470, time:.2 } );
				
				if (this.isGameResult == true) {
					
					Tweener.addTween(this.gameResult, { x: 500, time:.2 } );
				}
				
			}else {
				Tweener.addTween(this.gameAreaControl, { x: 387.4, time:.2 } );
				
				Tweener.addTween(this.middle_mc, { x: 360, time:.2 } );
				Tweener.addTween(this.rb_mc, { x: 780, time:.2 } );
				Tweener.addTween(this.rt_mc, { x: 780, time:.2 } );
				
				Tweener.addTween(this.gameAreaControl.control_btns, { x: 370, time:.2 } );
				
				if (this.isGameResult == true) {
					
					Tweener.addTween(this.gameResult, { x: 390, time:.2 } );
				}
				
			}
			
		}
		
		
		
		
		
		
	}
	
}